//
//  SleepSortCell.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/26.
//

#import "SleepSortCell.h"
#import "SleepGroupListCell.h"
#import "SleepListVC.h"
#import "SleepMusicVC.h"

@interface SleepSortCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * listView;
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * infoLabel;
@property (nonatomic,strong) UIButton * enterBtn;
@property (nonatomic,strong) NSMutableArray * listArray;
@property (nonatomic,strong) SleepGroupModel * model;
@end

@implementation SleepSortCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setSubViewLayout];
    }
    return self;
}

-(void)setSubViewLayout{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20 * FMScaleX);
        make.top.mas_offset(10 * FMScaleX);
    }];
    [self addSubview:self.infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(2 * FMScaleX);
    }];
    [self addSubview:self.enterBtn];
    [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10 * FMScaleX);
        make.size.mas_offset(CGSizeMake(30 * FMScaleX, 30 * FMScaleX));
        make.top.equalTo(self.nameLabel.mas_top).offset(13 * FMScaleX);
    }];
    
    [self addSubview:self.listView];
    [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-20 * FMScaleX);
        make.left.mas_offset(20 * FMScaleX);
        make.top.equalTo(self.infoLabel.mas_bottom).offset(10 * FMScaleX);
        make.size.mas_offset(CGSizeMake(WIDTH - 40 * FMScaleX, 140 * FMScaleX));
    }];
}

-(void)setInfoModel:(SleepGroupModel *)infoModel{
    
    self.model = infoModel;
    self.nameLabel.text = infoModel.title;
    self.infoLabel.text = infoModel.describe;
    [self.listArray removeAllObjects];
    [self.listArray addObjectsFromArray:infoModel.sleepMusicRecordList];
    [self.listView reloadData];
}

-(void)enterBtnAction{
    
    SleepListVC * vc = [[SleepListVC alloc]init];
    vc.typeId = self.model.ID;
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.listArray.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    SleepGroupListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SleepGroupListCell" forIndexPath:indexPath];
    SleepGroupListModel * model = self.listArray[indexPath.row];
    if (model) {
        [cell setInfoMdoel:model];
    }
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(5* FMScaleX, 0* FMScaleX, 5* FMScaleX, 0* FMScaleX);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SleepGroupListModel * model = self.listArray[indexPath.row];
    if (model) {
        SleepMusicVC * vc = [[SleepMusicVC alloc]init];
        vc.modalPresentationStyle = UIModalPresentationFullScreen;
        vc.infoModel = model;
        vc.listArray = [self.listArray copy];
        [[UIViewController findNavigationController] presentViewController:vc animated:YES completion:nil];
    }
    
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = Semibold(18);
        _nameLabel.textColor = UIColor.whiteColor;
    }
    return _nameLabel;
}
-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]init];
        _infoLabel.font = Regular(13);
        _infoLabel.textColor =UIColor.whiteColor;
    }
    return _infoLabel;
}
-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]init];
        [_enterBtn setImage:[UIImage imageNamed:@"Me_head_arrow"] forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterBtn;
}

-(UICollectionView *)listView{

    if (!_listView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 20 * FMScaleX;
        layout.minimumInteritemSpacing = 20 * FMScaleX;
        layout.itemSize = CGSizeMake(120 * FMScaleX, 120 * FMScaleX);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _listView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        [_listView registerClass:[SleepGroupListCell class] forCellWithReuseIdentifier:@"SleepGroupListCell"];
    }
    return _listView;
}
-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
@end
